import Dexie, { Table } from 'dexie'
import {
  ProtocolsConfig,
  ScanPartsConfig,
  MonitorProtocolConfig,
  PreMonitorProtocolConfig
} from '@renderer/store/db/dbPresetConfig'


interface ScanPartsType {
  id?: number;
  personType: string,
  parts: any[]
}

interface ProtocolsType {
  id?: number;
  name: string;
  scanType: string;
  scanPart: string;
  params: any
}


export class MyDB extends Dexie {

  scanParts: any
  protocols: any
  preMonitorProtocol: any
  monitorProtocol: any
  preset: any

  constructor() {
    super('mydb')
    this.version(6).stores({
      scanParts: '++id, name',
      protocols: '++id,name',
      monitorProtocol: '++id',
      preMonitorProtocol: '++id'
    })
      .upgrade(() => {
        // 版本升级触发。
        this.protocols.clear()
        this.initDb()
      })
    // 监听db初始化创建， 只会由open触发。
    this.on('populate', () => {
      this.initDb()
    })
    this.open()
  }

  initDb() {
    this.scanParts.bulkAdd(ScanPartsConfig)
    this.protocols.bulkAdd(ProtocolsConfig)
    this.monitorProtocol.add(MonitorProtocolConfig)
    this.preMonitorProtocol.add(PreMonitorProtocolConfig)
  }

  init() {
  }
}

const db = new MyDB()
export {
  db
}
export type {
  ScanPartsType,
  ProtocolsType
}


